package fr.curriculumvitae.anthony.stephan.activities;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.LinearLayout;
import fr.curriculumvitae.anthony.stephan.R;
import fr.curriculumvitae.anthony.stephan.objects.Message;

public class MessageDetailsActivity extends Activity {
	
	public static final String EXTRA_MESSAGE = "message";
	private static final String EMPTY = "";
	
	private Message message;
	
	private TextView tvName;
	private TextView tvFunction;
	private TextView tvEnterprise;
	private TextView tvEmail;
	private TextView tvPhone;
	private TextView tvMessage;
	private Button bActionRead;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		// dismiss action bar
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		// get extras
		Bundle extras = getIntent().getExtras();
		if(extras != null) {
			message = (Message) extras.get(EXTRA_MESSAGE);
		} else {
			finish();
		}
		
        // set content view
		setContentView(R.layout.activity_message_display);
		
		// adapt width & height (with max limits if needed)
        final LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout_contact_form);
        ViewTreeObserver vto = layout.getViewTreeObserver(); 
        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
            @SuppressWarnings("deprecation")
			@Override 
            public void onGlobalLayout() { 
            	layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                DisplayMetrics metrics = getResources().getDisplayMetrics();
                int activityFinalWidth = (int) (metrics.widthPixels * 0.90);
                int activityFinalHeight = (int) (metrics.heightPixels * 0.75);
                getWindow().setLayout(activityFinalWidth, activityFinalHeight);
            } 
        });
        
        fetchViews();
        customizeViews();
        setListeners();
	}
	
	
	private void fetchViews() {
		tvName = (TextView) findViewById(R.id.editText_contact_form_name);
		tvFunction = (TextView) findViewById(R.id.editText_contact_form_function);
		tvEnterprise = (TextView) findViewById(R.id.editText_contact_form_enterprise);
		tvEmail = (TextView) findViewById(R.id.editText_contact_form_mail);
		tvPhone = (TextView) findViewById(R.id.editText_contact_form_phone);
		tvMessage = (TextView) findViewById(R.id.editText_contact_form_message);
		bActionRead = (Button) findViewById(R.id.button_contact_form_action_read);
	}
	
	private void customizeViews() {
		if(message != null) {
			tvName.setText(message.getName());
			tvFunction.setText(message.getFunction());
			tvEnterprise.setText(message.getEnterprise());
			tvEmail.setText(message.getEmail());
			tvPhone.setText(message.getPhone());
			tvMessage.setText(message.getMessage());
		} 
	}
	
	private void setListeners() {
		bActionRead.setOnClickListener(onSendClickListener);
		tvEmail.setOnClickListener(onEmailClickListener);
		tvPhone.setOnClickListener(onPhoneClickListener);
	}
	
	
	@Override
	public void onDestroy() {
		super.onDestroy();
	}
	
	
	  ///////////////
	 // LISTENERS //
	///////////////
	
	private OnClickListener onSendClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			Intent result = new Intent();
			if(!message.isRead()) {
				result.putExtra(EXTRA_MESSAGE, message);
				setResult(RESULT_FIRST_USER, result);
			} else {
				setResult(RESULT_OK);
			}
			finish();
		}
	};
	
	private OnClickListener onEmailClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			String mail = message.getEmail();
			if(mail != null && !mail.equals(EMPTY)) {
				Intent mailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto", mail, null));
				startActivity(Intent.createChooser(mailIntent, getResources().getString(R.string.answer_by_email)));
			}
		}
	};
	
	private OnClickListener onPhoneClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			String phone = message.getPhone();
			if(phone != null && !phone.equals(EMPTY)) {
				Intent callIntent = new Intent(Intent.ACTION_CALL);          
	            callIntent.setData(Uri.parse("tel:"+phone));          
	            startActivity(callIntent);  
			}
		}
	};
	
}
